home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / c_course.zip / DOSEX.C < prev    next >
Text File  |  1989-12-30  |  9KB  |  259 lines

  1. /******************************************************************/
  2. /* This is an example program to illustrate how to;               */
  3. /*  1. Get the time and date from DOS                             */
  4. /*  2. Set the cursor to any position on the screen               */
  5. /*  3. Read characters from the keyboard and display their codes  */
  6. /*  4. How to scroll a window up on the monitor                   */
  7. /*  5. Format a program for ease of reading and understanding     */
  8. /******************************************************************/
  9.  
  10. #include "stdio.h"
  11. #include "dos.h"
  12.  
  13. main()
  14. {
  15. int hour, minute, sec, old_sec;
  16. int character;
  17.  
  18.    draw_box();              /* draw the boxes around the fields */
  19.    old_sec = 0;             /* this variable stores the old time
  20.                                so we can look for a change      */
  21.  
  22.    do {
  23.       if (kbhit()) {                     /* has a key been hit? */
  24.          character = getch();            /* read it in          */
  25.          disp_char(character);           /* display it          */
  26.       }
  27.  
  28.       get_time(&hour,&minute,&sec);      /* get the time of day */ 
  29.       if (sec != old_sec) {              /* if it has changed,  */
  30.          disp_time_date();               /* update the display  */
  31.          old_sec = sec;                  /* save new time       */
  32.       }
  33.  
  34.    } while (character != 'Q');        /* Quit when a Q is found */
  35.  
  36.    pos_cursor(0,0);              /* put cursor at top of screen */
  37. }
  38.  
  39.  
  40. /* **************************************************** drawbox */
  41. /* This routine draws a box on the screen. The keys hit, and    */
  42. /* the time and date are displayed in these boxes. There is     */
  43. /* nothing special about these boxes, they are simply output    */
  44. /* using the printf function.                                   */
  45. /* ************************************************************ */
  46. draw_box()
  47. {
  48. int index;
  49. char line[81];
  50.  
  51.    for (index = 0;index < 80;index++)       /* three blank rows */
  52.       line[index] = ' ';
  53.    line[80] = NULL;                            /* end of string */
  54.    for (index = 0;index < 3;index++)
  55.       printf("%s",line);
  56.  
  57.    line[8] = 201;                       /* draw top line of box */
  58.    for (index = 9;index < 70;index++)
  59.       line[index] = 205;
  60.    line[70] = 187;
  61.    printf("%s",line);
  62.  
  63.    line[8] = 186;                    /* draw sides of large box */
  64.    for (index = 9;index < 70;index++)
  65.       line[index] = ' ';
  66.    line[70] = 186;
  67.    for (index = 0;index < 15;index++)
  68.       printf("%s",line);
  69.  
  70.    line[8] = 204;                    /* draw line between boxes */
  71.    for (index = 9;index < 70;index++)
  72.       line[index] = 205;
  73.    line[70] = 185;
  74.    printf("%s",line);
  75.  
  76.    line[8] = 186;                    /* sides for time/date box */
  77.    for (index = 9;index < 70;index++)
  78.       line[index] = ' ';
  79.    line[70] = 186;
  80.    printf("%s",line);
  81.  
  82.    line[8] = 200;                     /* bottom line of the box */
  83.    for (index = 9;index < 70;index++)
  84.       line[index] = 205;
  85.    line[70] = 188;
  86.    printf("%s",line);
  87.  
  88.    for (index = 0;index < 80;index++)       /* three blank rows */
  89.       line[index] = ' ';
  90.    for (index = 0;index < 3;index++)
  91.       printf("%s",line);
  92.  
  93. }
  94.  
  95.  
  96. /* ************************************************** disp_char */
  97. /* This routine displays the characters hit on the monitor. If  */
  98. /* the first character is a zero, a special character has been  */
  99. /* hit, and the zero is displayed. The next character is read,  */
  100. /* and it is displayed on the monitor.                          */
  101. /* ************************************************************ */
  102. disp_char(inchar)
  103. int inchar;
  104. {
  105.    scroll_window();
  106.    pos_cursor(17,15);          /* position of message on screen */
  107.  
  108.    if(inchar == 0) {
  109.       printf(" 00 ");            /* a special character was hit */
  110.       inchar = getch();          /* get the next part of it     */
  111.       switch (inchar) {
  112.          case 59  :
  113.          case 60  :
  114.          case 61  :
  115.          case 62  :
  116.          case 63  :              /* these are the function keys */
  117.          case 64  :
  118.          case 65  :
  119.          case 66  :
  120.          case 67  :
  121.          case 68  : printf("%4d Function key F%d\n",inchar,inchar-58);
  122.                     break;
  123.  
  124.          case 94  :
  125.          case 95  :
  126.          case 96  :
  127.          case 97  :
  128.          case 98  :         /* these are the ctrl-function keys */
  129.          case 99  :
  130.          case 100 :
  131.          case 101 :
  132.          case 102 :
  133.          case 103 : printf("%4d Function key Ctrl-F%d\n",inchar,
  134.                        inchar-93);
  135.                     break;
  136.  
  137.          case 84  :
  138.          case 85  :
  139.          case 86  :
  140.          case 87  :        /* these are the upper-function keys */
  141.          case 88  :
  142.          case 89  :
  143.          case 90  :
  144.          case 91  :
  145.          case 92  :
  146.          case 93  : printf("%4d Function key Upper-F%d\n",inchar,
  147.                        inchar-83);
  148.                     break;
  149.  
  150.          case 104 :
  151.          case 105 :
  152.          case 106 :
  153.          case 107 :
  154.          case 108 :          /* these are the alt-function keys */
  155.          case 109 :
  156.          case 110 :
  157.          case 111 :
  158.          case 112 :
  159.          case 113 : printf("%4d Function key Alt-F%d\n",inchar,
  160.                        inchar-103);
  161.                     break;
  162.  
  163.          default  : printf("%4d Special key hit\n",inchar);
  164.       }
  165.  
  166.    } else                        /* a regular character was hit */ 
  167.       printf("    %4d (%c) Character Hit.\n",inchar,inchar);
  168.  
  169.    pos_cursor(25,1);        /* hide the cursor on the 26th line */
  170. }
  171.  
  172.  
  173. /* *************************************************** get_time */
  174. /* This routine calls the DOS function call for time of day. It */
  175. /* returns the time of day to the calling program in the three  */
  176. /* pointers used in the call.                                   */
  177. /* ************************************************************ */
  178. get_time(hour,minute,second)
  179. int *hour, *minute, *second;
  180. {
  181. union REGS inregs;
  182. union REGS outregs;
  183.  
  184.    inregs.h.ah = 44;               /* Hex 2C - Get current time */
  185.    int86(0x21,&inregs,&outregs);
  186.    *hour = outregs.h.ch;
  187.    *minute = outregs.h.cl;
  188.    *second = outregs.h.dh;
  189. }
  190.  
  191.  
  192. /* ********************************************* disp_time_date */
  193. /* This routine displays the time and date on the monitor in a  */
  194. /* fixed position. It gets the time from the get_time function, */
  195. /* and gets the date from its own built in DOS call. Good       */
  196. /* programming practice would move the date to another function */
  197. /* but this is an illustrative example to display methods of    */
  198. /* doing things. This routine also calls the cursor positioning */
  199. /* function to put the time and date where we want them.        */
  200. /* ************************************************************ */
  201. disp_time_date()
  202. {
  203. int hour, minute, second;
  204. union REGS inregs;
  205. union REGS outregs;
  206.  
  207.    pos_cursor(19,19);  /* position the cursor for date and time */
  208.  
  209.    inregs.h.ah = 42;              /* hex 2A - What is the date? */
  210.    int86(0x21,&inregs,&outregs);  /* interrupt 21               */      
  211.    printf("Date = %2d/%2d/%2d    ",
  212.       outregs.h.dh,                 /* month - 1 to 12          */
  213.       outregs.h.dl,                 /* day - 1 to 31            */
  214.       outregs.x.cx);                /* year - 1980 to 2099      */
  215.  
  216.    get_time(&hour, &minute, &second);
  217.    printf("Time = %2d:%2d:%2d\n",hour, minute, second);
  218.  
  219.    pos_cursor(25,1);        /* hide the cursor on the 26th line */
  220. }
  221.  
  222. /* ************************************************* pos_cursor */
  223. /* This routine positions the cursor at the requested row and   */
  224. /* column. The upper left corner is row 0 and column 0          */
  225. /* ************************************************************ */
  226. pos_cursor(row,column)
  227. char row, column;
  228. {
  229. union REGS inregs;
  230. union REGS outregs;
  231.  
  232.    inregs.h.ah = 2;        /* service 2 - position the cursor   */
  233.    inregs.h.dh = row;
  234.    inregs.h.dl = column;
  235.    inregs.h.bh = 0;
  236.    int86(0x10,&inregs,&outregs);                /* interrupt 10 */
  237. }
  238.  
  239.  
  240. /* ********************************************** scroll_window */
  241. /* This routine scrolls all of the material in the key hit      */
  242. /* window up one space leaving room for another entry.          */
  243. /* ************************************************************ */
  244. scroll_window()
  245. {
  246. union REGS inregs;
  247. union REGS outregs;
  248.  
  249.    inregs.h.ah = 6;      /* service 6 - scroll window           */
  250.    inregs.h.al = 1;      /* number of lines to scroll           */
  251.    inregs.h.ch = 3;      /* top row of window                   */
  252.    inregs.h.cl = 9;      /* left column of window               */
  253.    inregs.h.dh = 17;     /* bottom row of window                */
  254.    inregs.h.dl = 69;     /* right column of window              */
  255.    inregs.h.bh = 7;      /* attribute of blank line             */
  256.    int86(0x10,&inregs,&outregs);                /* interrupt 10 */
  257.  
  258. }
  259.